const express = require('express');
const FocusModel = require('../model/focusModel')
const NavModel = require('../model/navModel')
const ArticleModel = require("../model/articleModel");
const ArticleCateModel = require("../model/articleCateModel");
const url = require('url');
const mongoose = require("../model/core");
const tools = require('../model/tools');
const articleModel = require('../model/articleModel');
const SettingModel = require('../model/settingModel');
var router = express.Router();


router.use(async (req,res,next)=>{

  var pathname = url.parse(req.url).pathname;
  //获取公共的数据
  var navResult = await NavModel.find({"position":2}).sort({'sort':-1});
  req.app.locals.navList = navResult;//把数据放到全局变量中
  req.app.locals.pathname = pathname;//存放路径是为了实现点击导航选中
  req.app.locals.formatTime = tools.formatTime//将格式化时间放到全局变量中

  req.app.locals.unixToDay = tools.unixToDay;
  req.app.locals.unixToYearAndMonth = tools.unixToYearAndMonth;
  req.app.locals.title="ly后台",
  req.app.locals.keywords="lycode",
  req.app.locals.description="lycode",
  next();
})



router.get("/index.html",async (req,res)=>{
  //轮播图
  var focusResult = await FocusModel.find({"type":1}).sort({"sort":-1})
  //展会新闻
  var topNewsList = await articleModel.find({"cid":mongoose.Types.ObjectId("619f0062c080b0ade3906d1e")},"title link").limit(4).sort({"sort":-1})
  //推荐新闻
  var bestNewsList = await articleModel.find({"is_best":1}).limit(4).sort({"sort":-1}) 
  
  //获取系统配置
  var settingResult = await SettingModel.find({});

  res.render("default/index.html",{
    focusList:focusResult,
    topNewsList:topNewsList,
    bestNewsList:bestNewsList,
    title:settingResult[0].site_title,
    keywords:settingResult[0].site_keywords,
    description:settingResult[0].site_description,
  })
})

router.get("/overview.html",(req,res)=>{
  res.render("default/overview.html")
})

router.get("/news.html",async (req,res)=>{
  //获取新闻数据
  var page = req.query.page || 1;
  var cid = req.query.cid || "";
  var pageSize = 2//每一页显示多少数据
  var json={} //条件

  //获取新闻中心的子分类,防止别的分类也展示在新闻中心
  if(cid){
    json=Object.assign(json,{"cid":mongoose.Types.ObjectId(cid)})
   
  }else{
    var cateResult = await ArticleCateModel.find({"pid":mongoose.Types.ObjectId("619f021ac080b0ade3906d3b")})

    var tempArr = [];//将新闻中心下面所有的子分类的id存放到数组中
    cateResult.forEach((value)=>{
      tempArr.push(value._id)
    })
    json=Object.assign(json,{
      "cid":{$in:tempArr}//$in判断在什么之中,这里判断文章表中的cid(分类id)是否在tempArr新闻中心的子分类中
    })
  }

  var result = await ArticleModel.aggregate([
      {
          $lookup: {
              from: "article_cate",
              localField: "cid",
              foreignField: "_id",
              as: "cate"
          }
      },
      {
          $match:json//如果需要做搜索的话，这里可以写搜索的条件
      },
      {
          $sort: {"add_time": -1}
      },{
          $skip: (page - 1) * pageSize
      },{
          $limit: pageSize
      }
  ]);

  var count = await ArticleModel.count(json);
  res.render("default/news.html",{
      newsList:result,
      totalPages:Math.ceil(count / pageSize),
      page:page,
      cid:cid
  })
 
})

router.get("/services.html",async (req,res)=>{
  //获取展会服务下面的信息 如果可以循环
  //第一种方法直接获取展会服务下全部子分类和文章,根据页面布局选择用那种方法
  // var result = await ArticleCateModel.aggregate([
  //   {
  //       $lookup: {
  //           from: "article",
  //           localField: "_id",
  //           foreignField: "cid",
  //           as: "articleList"
  //       }
  //   },
  //   {
  //       $match:{"pid":mongoose.Types.ObjectId("619f0062c080b0ade3906d1e")}
  //   },
  //   {
  //       $sort: {"add_time": -1}
  //   }
  // ]);
  // console.log(JSON.stringify(result,undefined,'\t'))

  //第二种方法一个一个的获取展会服务下子分类和文章 第二个参数可以指定获取字段
  
  //资料下载
  var list1 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f0086c080b0ade3906d22")},"title link")
  //平面图
  var list2 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f0095c080b0ade3906d26")},"title link")
  //交通信息
  var list3 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f00a0c080b0ade3906d2a")},"title link")
  //票务信息
  var list4 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f3da87de2cb5996cca956")},"title link")
  //周边服务
  var list5 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f3dc97de2cb5996cca95c")},"title link")
  //同期活动
  var list6 = await articleModel.find({"cid":mongoose.Types.ObjectId("619f3ddb7de2cb5996cca960")},"title link")
  
  res.render("default/services.html",{
    list1,
    list2,
    list3,
    list4,
    list5,
    list6
  })
})

router.get("/contact.html",(req,res)=>{
  res.render("default/contact.html")
})

router.get("/content_:id.html",async (req,res)=>{
  var id= req.params.id;

  var result = await ArticleModel.find({"_id":id})
  res.render("default/news_content.html",{
    list:result[0]
  })
})
module.exports = router